import config
import urllib3,dohttp
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def wifi_logout():
    print(f"注销返回：{sendreq("opr=logout&ipv4or6=")}")
def wifi_auth(username,password):
    go = 0
    wifi_logout()#切换不同账户时需要先注销
    msg = sendreq(f"opr=pwdLogin&userName={username}&pwd={password}&ipv4or6=&rememberPwd=1")
    if msg.find("logon success")!= -1 or msg.find("已在线")!=-1:
        ret ="登陆成功"
        go = 1
    elif msg.find("NOAUTH")!=-1:
        ret = "无限流时间"
        go = 1
    elif msg.find("冻结") != -1:
        ret = "登陆失败，登录频繁，账户被冻结一分钟"
    elif msg == "error":
        ret = "登陆失败，可能是未连接校园网，或者是校园网和流量同时打开导致无法认证"
    else:
        ret = "登陆失败，可能是密码错误"
    print(msg)
    return go
def sendreq(s_data:str)->str:
    Header = {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0"   
        }
    res = dohttp.post(url="https://a.stu.edu.cn/ac_portal/login.php",headers=Header,data=s_data,verify=False)
    if res == "error":
        return "error"
    elif res.status_code == 200:
        return res.content.decode("utf-8")
    else:
        return "error"
if __name__ == '__main__':
    username = config.gcon("setting","name")
    password = config.gcon("setting","password")
    msg = sendreq(f"opr=pwdLogin&userName={username}&pwd={password}&ipv4or6=&rememberPwd=1")
    if "logon success" in  msg or "已在线" in msg:
        ret ="登陆成功"
    elif "NOAUTH" in msg:
        ret = "无限流时间"
    elif msg == "error":
        ret = "登陆失败，可能是未连接校园网，或者是校园网和流量同时打开导致无法认证"
    else:
        ret = "登陆失败，可能是密码错误"
    print(ret)

